<template>
  <el-dialog :title="title" class="freezeAccount" :visible.sync="isShowModel" :before-close="handleClose" @close="$emit('update:isShowDetail', false)" :close-on-click-modal="false">
    <el-form :model="breakInfo" ref="breakInfo" :rules="rules" label-width="140px">
      <el-form-item label="进行违约:" required>
        <div class="c-red">
          违约操作
        </div>
      </el-form-item>
      <el-form-item label="采购商企业名称:" required>
        <div>
          {{dataInfo ? dataInfo.accountName : ""}}
        </div>
      </el-form-item>
      <el-form-item label="操作验证:" required>
        <el-input v-model="dataInfo.gmTel" disabled>
          <template slot="append">
            <el-button type="text" @click="getPhoneWord" v-if="!isHasValidCode" :loading="isGetInterface">
              发送验证码
            </el-button>
            <span class="" v-else>{{tipMessage}}</span>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="验证验证码:" prop="smsCode" required>
        <el-input v-model="breakInfo.smsCode"></el-input>
      </el-form-item>
      <el-form-item label="备注内容:" prop="breakRemark" required>
        <el-input v-model="breakInfo.breakRemark" type="textarea" :autosize="{ minRows: 6, maxRows: 8}"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeModel">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  export default {
    name: "Treaty",
    props: {
      isShowDetail: {
        type: Boolean,
        default: false
      },
      dataInfo: {
        type: Object,
        default: () => {}
      },
      title: {
        type: String,
        default: "违约操作"
      }
    },
    data() {
      return {
        isShowModel: this.isShowDetail,
        breakInfo: {
          smsCode: "",
          breakRemark: ""
        },
        timer: null,
        tipMessage: "",
        rules: {
          smsCode: [
            { required: true, message: '请输入验证码', trigger: ['change', 'blur'] }
          ],
          breakRemark: [
            { required: true, message: '请输入备注内容', trigger: ['change', 'blur'] },
            { validator: validate.length150, trigger: ['change', 'blur'] }
          ]
        },
        isHasValidCode: false,
        // 是否在请求接口
        isGetInterface: false
      }
    },
    methods: {
      getPhoneWord() {
        // this.$message.error("获取手机验证码");
        this.isHasValidCode = true;
        let time = 60;
        this.tipMessage = time + "秒";
        setInterval(() => {
          if(time <= 0) {
            clearInterval(this.timer)
            this.timer = null;
            this.isHasValidCode = false
            this.tipMessage = ""
            return ;
          }
          time --;
          this.tipMessage = time + "秒";
        }, 1000)
        let data = {
          id: this.dataInfo.id
        }
        ajax.get(apis.factory.sendBreakSms, data).then(res => {
          if(res.code == "10000000") {

          } else {
            clearInterval(this.timer)
            this.timer = null;
            this.$message.error(res.message)
          }
        })
      },
      handleClose(done) {
        clearInterval(this.timer)
        this.timer = null;
        this.isHasValidCode = false;
        this.$refs['breakInfo'].resetFields()
        done()
      },
      closeModel() {
        clearInterval(this.timer)
        this.timer = null;
        this.isHasValidCode = false;
        this.$refs['breakInfo'].resetFields()
        this.$emit('update:isShowDetail', false)
      },
      submitForm() {
        let data = {
          ...this.breakInfo,
          id: this.dataInfo.id
        }
        this.$refs['breakInfo'].validate((valid) => {
          if(valid) {
            ajax.post(apis.factory.setBreakState, data).then(res => {
              if(res.code == "10000000") {
                this.$message({
                  type: 'success',
                  message: '设置违约成功'
                });
                this.$emit('fresh')
                this.closeModel()
              } else {
                this.$message.error(res.message)
              }
            })
          }
        })
      }
    },
    watch: {
      isShowDetail(val) {
        this.isShowModel = val
      }
    }
  }
</script>

<style lang="scss">
  .freezeAccount {
    .el-dialog {
      width: 600px;
      .el-input {
        width: 300px;
        .el-button {
          padding: 0 10px;
        }
      }
    }
  }
</style>
